package com.yourname.action;

import com.yourname.custom.CustomCourseList;
import com.yourname.custom.CustomCourseStudentList;
import com.yourname.custom.CustomScoreStudentList;
import com.yourname.entity.*;
import com.yourname.store.DataStore;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CourseAction {
    private static Scanner scanner=new Scanner(System.in);

    /**
     * 创建课程
     * @return 是否创建成功，成功为true，失败为false
     */
    public static boolean createCourse(){
        System.out.println("请输入课程编号、课程名、负责教师的教职工号，中间用-分割");
        String next = scanner.next();
        String[] splited = next.split("-");
        Teacher teacher = DataStore.getTeacher(splited[2]);//先根据教职工号判断老师是否存在
        if (teacher!=null){//老师存在则存储新课程
            Course course=new Course();
            course.setId(splited[0]);
            course.setTid(teacher.getId());
            course.setName(splited[1]);
            DataStore.addCourse(course);
            DataStore.saveData();
            return true;
        }else{//老师不存在则告知老师不存在，无法添加
            System.out.println("课程添加失败，教师"+splited[2]+"不存在");
            return false;
        }
    }

    /**
     * 编辑课程
     * @return 编辑成功为true，否则为false
     */
    public static boolean editCourse(){
        System.out.println("请输入要修改课程的课程编号、新课程名、新的负责教师的教职工号，中间用-分割");
        String next = scanner.next();
        String[] splited = next.split("-");
        Teacher teacher = DataStore.getTeacher(splited[2]);
        if (teacher!=null){
            Course origin = DataStore.getCourse(splited[0]);//判断用户输入的课程号是否存在，如果课程不存在则无法编辑
            if (origin!=null){
                origin.setTid(teacher.getId());
                origin.setName(splited[1]);
                DataStore.updateCourse(origin);
                DataStore.saveData();
                return true;
            }else {
                System.out.println("课程修改失败，课程"+splited[0]+"不存在");
            }

        }else{
            System.out.println("课程添加失败，教师"+splited[2]+"不存在");
        }
        return true;
    }

    /**
     * 选课，需要接受学生的学号。
     * @param sid 学号
     */

    public static void chooseCourse(String sid){
        System.out.println("请输入课程编号：");
        String courseId = scanner.next();
        Course course = DataStore.getCourse(courseId);//判断用户输入的课程是否存在
        if (course!=null){//存在则进行选课
            CourseStudent query=new CourseStudent();
            query.setCid(courseId);
            query.setSid(sid);
            //判断学生是否已经选过该课
            CourseStudent courseStudentBySid = DataStore.getCourseStudentByAll(query);
            if (courseStudentBySid==null){//学生没有选过这个课程
                courseStudentBySid=new CourseStudent();
                courseStudentBySid.setSid(sid);
                courseStudentBySid.setCid(courseId);
                DataStore.addCourseStudent(courseStudentBySid);
                DataStore.saveData();
            }else{
                System.out.println("添加失败，该学生已经选过该课程");
            }
        }else{
            System.out.println("添加失败，该课程不存在");
        }

    }

    /**
     * 退课
     * @param sid 传入学号来判断该学生是否选了课程
     */
    public static void quitCourse(String sid){
        System.out.println("请输入课程编号：");
        String cid = scanner.next();
        CourseStudent courseStudent=new CourseStudent();
        courseStudent.setSid(sid);
        courseStudent.setCid(cid);
        CourseStudent log = DataStore.getCourseStudentByAll(courseStudent);
        if (log!=null){
            DataStore.removeCourseStudent(log);
            DataStore.saveData();
        }else{
            System.out.println("退出失败，该学生没有选过该课程");
        }

    }
    public static void getCourseList(String sid){
        CustomCourseStudentList allCourseStudent = DataStore.getAllCourseStudent();
        int index=1;
        for (CourseStudent cs:allCourseStudent){
            if (cs.getSid().equals(sid)){
                Course course = DataStore.getCourse(cs.getCid());
                System.out.printf("%d. %s-%s\n",index++,course.getId(),course.getName());
            }
        }
    }

}
